/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-02-26 15:59:57
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-21 11:35:20
 */
// pages/note/index.js
const app = getApp()
import {
  noteAPI
} from "./../../api/note.js"

import {
  returnStatus
} from '../../utils/util'
import {
  globalAPI
} from "../../api/index.js"

const api = {
  find: '/note/discovery',
  recommend: '/note/recommendation'
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initImg: '/pages/images/1.jpg',
    hasNewMsg: false,
    value: '',
    isResult: false,
    showReadPopup: false,
    open: false,
    wait: false,
    notes: [],
    pageNum: 1,
    pages: 1,
    activeTab: 'find',
    api: api.find,
    requestObj: {},
    showShear: false,
    nvabarData: {
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
    },
  },

  closeReadPopup() {
    this.setData({
      showReadPopup: false
    })
  },

  onOpenRead() {
    this.setData({
      open: true
    })
  },

  handOpen() {
    this.setData({
      wait: true
    })
    setTimeout(() => {
      this.setData({
        open: true
      })
    }, 500);
  },

  // 切换Tab
  onChangeTab({
    detail: {
      name
    }
  }) {
    this.setData({
      api: api[name],
      pageNum: 1,
      notes: []
    })
    this.getData()
  },
  toSearch() {
    wx.navigateTo({
      url: '/pages/search/index',
    });
  },
  toDetail({
    currentTarget
  }) {
    const {
      noteid
    } = currentTarget.dataset

    wx.navigateTo({
      url: `detail/index?noteId=${noteid}`,
    })
  },
  toMsg() {
    wx.navigateTo({
      url: '/pages/mine/msg/index',
      success: (result) => {
        result.eventChannel.emit('props', {
          type: 'info'
        })
      },
      fail: () => {},
      complete: () => {}
    });
  },
  // 关闭分享
  closeShear() {
    this.setData({
      showShear: false
    })
  },
  // 复制链接
  copyLink() {
    wx.setClipboardData({
      //去找上面的数据
      data: `http://app.davhaowu.com/home/note/${this.noteId}`,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    })
  },
  // 请求数据
  getData(pageSize = 10) {
    const {
      pageNum,
      api: url
    } = this.data
    return new Promise((resolve, reject) => {
      const getRes = noteAPI.getNotes({
        pageNum,
        url
      })
      getRes.then(res => {
        if (res.code === 200) {
          this.setData({
            notes: [...this.data.notes, ...res.data.records],
            pageNum: ++this.data.pageNum,
            pages: res.data.records.length
          })
          resolve(this.data.pages)
        } else {
          reject(false)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // app.setTitle("大V好物")
    const isIphoneX = app.globalData.isIphoneX
    this.setData({
      isIphoneX,
      width:app.globalData.width,
      height: app.globalData.height,
      requestObj: {
        fetchData: (refresh) => {
          refresh ? this.setData({
            pageNum: 1,
            notes: []
          }) : ''
          return this.getData()
        },
      }
    })
    setTimeout(() => {
      this.getData()
    }, 500);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (this.data.key || !wx.getStorageSync('token')) return
    setTimeout(() => {
      // 获取提醒事项
      noteAPI.queryRemind().then(async _ => {
        const res = await returnStatus(_)
        if (_.data > 0) {
          this.setData({
            hasNewMsg: res
          })
        }
      })
    }, 800);

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      console.log('设置选中项 0')
      this.getTabBar().setData({
        active: 0
      })
    }
    // 获取得到的笔记id
    if (app.data.globalParams) {
      this.setData({
        showShear: true
      })
    }
    this.noteId = app.data.globalParams
    app.data.globalParams = ''
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    delete app.data.key
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})